<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>双列表数据展示</title>
    <style>
        body {
            font-family: 'Microsoft YaHei', Arial, sans-serif;
            margin: 0;
            padding: 20px;
            background-color: #f5f5f5;
        }
        .container {
            max-width: 1200px;
            margin: 0 auto;
        }
        h1 {
            text-align: center;
            color: #333;
            margin-bottom: 30px;
        }
        .data-container {
            display: flex;
            gap: 20px;
            flex-wrap: wrap;
        }
        .data-column {
            flex: 1;
            min-width: 300px;
            background: white;
            border-radius: 8px;
            padding: 20px;
            box-shadow: 0 2px 4px rgba(0,0,0,0.1);
        }
        .data-column h2 {
            margin-top: 0;
            color: #2c3e50;
            border-bottom: 2px solid #3498db;
            padding-bottom: 10px;
        }
        table {
            width: 100%;
            border-collapse: collapse;
            margin-top: 15px;
        }
        th, td {
            padding: 12px;
            text-align: left;
            border-bottom: 1px solid #ddd;
        }
        th {
            background-color: #3498db;
            color: white;
            font-weight: bold;
        }
        tr:hover {
            background-color: #f5f5f5;
        }
        .loading {
            text-align: center;
            padding: 50px;
            color: #666;
        }
        .error {
            color: #e74c3c;
            text-align: center;
            padding: 20px;
            background-color: #ffeaa7;
            border-radius: 5px;
            margin: 20px 0;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>数据展示系统</h1>
        <div id="error" class="error" style="display: none;"></div>
        <div class="data-container">
            <div class="data-column" id="column1">
                <h2>表1数据</h2>
                <div class="loading">加载中...</div>
            </div>
            <div class="data-column" id="column2">
                <h2>表2数据</h2>
                <div class="loading">加载中...</div>
            </div>
        </div>
    </div>

    <script>
        // 页面加载后获取数据
        document.addEventListener('DOMContentLoaded', async () => {
            try {
                const response = await fetch('/api/data');
                if (!response.ok) {
                    throw new Error('网络响应错误');
                }
                const data = await response.json();
                
                // 渲染第一个表的数据
                if (data.tables.length > 0 && data.data.length > 0) {
                    renderTable('column1', data.tables[0], data.data[0]);
                }
                
                // 渲染第二个表的数据
                if (data.tables.length > 1 && data.data.length > 1) {
                    renderTable('column2', data.tables[1], data.data[1]);
                }
            } catch (error) {
                document.getElementById('error').textContent = '获取数据失败: ' + error.message;
                document.getElementById('error').style.display = 'block';
                console.error('获取数据失败:', error);
            }
        });

        // 渲染表格数据
        function renderTable(columnId, tableConfig, tableData) {
            const column = document.getElementById(columnId);
            column.querySelector('h2').textContent = tableConfig.name + ' 数据';
            
            // 创建表格
            const table = document.createElement('table');
            
            // 创建表头
            const thead = document.createElement('thead');
            const headerRow = document.createElement('tr');
            tableConfig.columns.forEach(col => {
                const th = document.createElement('th');
                let label = col.label;
                // 添加数据类型和主键标识
                if (col.type) {
                    label += ` (${col.type})`;
                }
                if (col.primaryKey) {
                    th.title = '主键';
                    th.style.backgroundColor = '#e74c3c';
                }
                th.textContent = label;
                headerRow.appendChild(th);
            });
            thead.appendChild(headerRow);
            table.appendChild(thead);
            
            // 创建表格体
            const tbody = document.createElement('tbody');
            
            if (tableData && tableData.length > 0) {
                tableData.forEach(row => {
                    const tr = document.createElement('tr');
                    tableConfig.columns.forEach(col => {
                        const td = document.createElement('td');
                        // 处理空值
                        const value = row[col.name] !== null && row[col.name] !== undefined ? row[col.name] : '';
                        
                        // 根据数据类型进行特殊处理
                        if (col.type && col.type.includes('DECIMAL') && typeof value === 'number') {
                            // 格式化数字显示
                            td.textContent = value.toLocaleString();
                        } else {
                            td.textContent = value;
                        }
                        
                        tr.appendChild(td);
                    });
                    tbody.appendChild(tr);
                });
            } else {
                // 显示无数据提示
                const noDataRow = document.createElement('tr');
                const noDataCell = document.createElement('td');
                noDataCell.colSpan = tableConfig.columns.length;
                noDataCell.textContent = '暂无数据';
                noDataCell.style.textAlign = 'center';
                noDataCell.style.color = '#666';
                noDataCell.style.padding = '20px';
                noDataRow.appendChild(noDataCell);
                tbody.appendChild(noDataRow);
            }
            
            table.appendChild(tbody);
            
            // 替换加载中的内容
            const loading = column.querySelector('.loading');
            if (loading) {
                loading.parentNode.replaceChild(table, loading);
            } else {
                column.appendChild(table);
            }
        }
    </script>
</body>
</html>